package com.my.service.imp;

import com.my.dao.GoodsDao;
import com.my.dao.ShopCartDao;
import com.my.dao.UserDao;
import com.my.pojo.Goods;
import com.my.pojo.ShopCart;
import com.my.pojo.User;
import com.my.service.IShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopCartServiceImpl implements IShopCartService {
    ShopCartDao shopCartDao;

    UserDao userDao;

    GoodsDao goodsDao;

    @Autowired
    public void setShopCartDao(ShopCartDao shopCartDao) {
        this.shopCartDao = shopCartDao;
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Autowired
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    @Override
    public Page<ShopCart> showShopCart(Pageable pageable, Integer userId) {
        return shopCartDao.findAll(
                (root, cq, cb) -> cb.equal(root.get("user"), userId), pageable);
    }

    @Override
    public List<ShopCart> showShopCart(Integer userId) {
        return shopCartDao.findAll(
                (root, cq, cb) -> cb.equal(root.get("user"), userId));
    }

    @Override
    public Integer addToShopCart(Integer userId, Integer goodsId, Integer num) {
        User user = userDao.findOne(userId);
        if (user == null || user.getStatus() == -1) {
            return -1;
        }

        Goods goods = goodsDao.findOne(goodsId);
        if (goods == null || goods.getStatus() == -1) {
            return -2;
        }

        if (goods.getStock() < num) {
            return -3;
        }

        ShopCart shopCart = shopCartDao.findOne(
                (root, cq, cb) -> cb.and(
                        cb.equal(root.get("user"), userId),
                        cb.equal(root.get("goods"), goodsId),
                        cb.equal(root.get("status"), 0)
                )
        );

        if (shopCart != null) {
            shopCart.setNum(shopCart.getNum() + num);
        } else {
            shopCart = new ShopCart();
            shopCart.setNum(num);
            shopCart.setGoods(goods);
            shopCart.setUser(user);
            shopCart.setStatus(0);
        }

        ShopCart res = shopCartDao.save(shopCart);
        return res == null ? -4 : res.getCartId();
    }

    @Override
    public Integer updateShopCart(Integer cartId, Integer num, Integer userId) {
        ShopCart shopCart = shopCartDao.findOne(cartId);
        if (shopCart == null || shopCart.getStatus() != 0) {
            return -1;
        }

        if (!userId.equals(shopCart.getUser().getUserId())) {
            return -2;
        }

        Goods goods = shopCart.getGoods();
        if (goods == null || goods.getStatus() != 0) {
            return -3;
        }

        if (goods.getStock() < num) {
            return -4;
        }

        shopCart.setNum(num);
        if (num <= 0) {
            shopCart.setStatus(1);
        }
        return shopCartDao.save(shopCart) == null ? -5 : 0;
    }

    @Override
    public Integer deleteFromShopCart(Integer cartId, Integer userId) {
        ShopCart shopCart = shopCartDao.findOne(cartId);
        if (shopCart == null || shopCart.getStatus() != 0) {
            return -1;
        }

        if (!userId.equals(shopCart.getUser().getUserId())) {
            return -2;
        }

        shopCart.setStatus(-1);
        return shopCartDao.save(shopCart) == null ? -3 : 0;
    }
}
